﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Specialized;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;

using Might.Infrastructure.DOM;
using Might.Infrastructure.Events;
using Might.Infrastructure.Services;

namespace Might.Core.DOM {       

    ///<summary>
    ///</summary>
    public class DomRoot : DomNode, IDom {
        ///<summary>
        ///</summary>
        public DomRoot() {
            CoreServices.EventAggregator.GetEvent<ProgressEvent>().Subscribe( ProgressEventsHandler , ThreadOption.UIThread , false );            
        }

        private const string anonymousUser = "Anonymous";
        private const string anonymousPassword = "";

        ///<summary>
        ///</summary>
        public event EndProgressEventHandler LoginFinished;
        ///<summary>
        ///</summary>
        public event EndProgressEventHandler LogoutFinished;

        public void ProgressEventsHandler(IProgressToken progressToken) {
            if (progressToken.EventType == typeof(LoginEvent)) {                
                if( progressToken.State is EndProgressEventArgs ) {
                    OnLoginFinished( progressToken );
                } else {                
                    progressToken.ProgressGo += progressToken_ProgressGo;
                }
            }
            if (progressToken.EventType == typeof(LogoutEvent)) {
                if( progressToken.State is EndProgressEventArgs ) {
                    OnLogoutFinished( progressToken );
                } else {
                    progressToken.ProgressGo += progressToken_ProgressGo;
                }
            }
        }

        private void progressToken_ProgressGo(IProgressToken sender, ProgressEventArgs e) {
            if (sender.EventType == typeof(LoginEvent)) {
                if (sender.State is EndProgressEventArgs) {
                    sender.ProgressGo -= progressToken_ProgressGo;
                    OnLoginFinished(sender);
                }
            }
            if (sender.EventType == typeof(LogoutEvent)) {
                if (sender.State is EndProgressEventArgs) {
                    sender.ProgressGo -= progressToken_ProgressGo;
                    OnLogoutFinished(sender);
                }
            }
        }

        protected virtual void OnLoginFinished(IProgressToken progressToken ) {
            if (LoginFinished != null) {
                LoginFinished( progressToken, progressToken.State as EndProgressEventArgs );
            }
        }

        protected virtual void OnLogoutFinished(IProgressToken progressToken ) {
            if (LogoutFinished != null) {
                RemoveChild( GroupNames.User , GetFirstChild( GroupNames.User ) );
                LogoutFinished( progressToken , progressToken.State as EndProgressEventArgs );
            }
        }


        ///<summary>
        ///</summary>
        public void LoginAsAnonymous() {
            Login(anonymousUser, anonymousPassword);
        }

        ///<summary>
        ///</summary>
        ///<param name="name"></param>
        ///<param name="pass"></param>
        public void Login(string name, string pass) {
            CoreServices.EventAggregator.GetEvent<LoginEvent>().Publish( new LoginData { Name = name , passwd = pass } );
        }

        ///<summary>
        ///</summary>
        ///<param name="ui"></param>
        public void Logout() {
            CoreServices.EventAggregator.GetEvent<LogoutEvent>().Publish( GetFirstChild( GroupNames.User ) as IUserInfo );
        }
        



        ///<summary>
        ///</summary>
        ///<param name="from"></param>
        ///<typeparam name="T"></typeparam>
        ///<returns></returns>
        public IList<T> GetItems<T>(IDomNode from) where T : IDomNode {
            throw new NotImplementedException();
            //return from.Children.Where(n => n is T).Select(n => (T)n).ToList();
        }

        ///<summary>
        ///</summary>
        ///<param name="from"></param>
        ///<typeparam name="T"></typeparam>
        ///<returns></returns>
        ///<exception cref="NotImplementedException"></exception>
        public IList<T> GetItemsRecursive<T>(IDomNode from) where T : IDomNode {
            throw new NotImplementedException();
        }
    }
}
